package com.tiennt.android.callnotes;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.tiennt.android.callnotes.db.DbAdapter;
import com.tiennt.android.callnotes.models.contentholders.ContactInfo;
import com.tiennt.android.callnotes.models.contentholders.ListNotesItem;
import com.tiennt.android.callnotes.models.listadapters.ListNotesAdapter;
import com.tiennt.android.callnotes.utils.ContactUtils;

public class ViewNotesActivity extends Activity implements
		OnCheckedChangeListener {

	private final String TAG = "ViewNotesActivity";
	private final boolean DBG = true;

	private ListView mListNotes;
	ListNotesAdapter mListNotesAdapter;
	public ModeCallback mModeCallback = new ModeCallback();
	private ActionMode mActionMode;

	public static final int REQUEST_ADD_OR_EDIT_NOTE = 1002;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_notes);

		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("All notes");

		initialComponents();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.view_notes, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in Action Bar clicked; go home
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.action_addnote:
			Intent intent2 = new Intent(this, AddNoteActivity.class);
			startActivityForResult(intent2, REQUEST_ADD_OR_EDIT_NOTE);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void initialComponents() {
		mListNotes = (ListView) findViewById(R.id.lvAllNotes);

		mListNotesAdapter = new ListNotesAdapter(this, getAllNotes());
		mListNotesAdapter.setOnCheckedChangeListener(this);
		mListNotes.setAdapter(mListNotesAdapter);

		mListNotes.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
		mListNotes.setMultiChoiceModeListener(mModeCallback);
	}

	private class ModeCallback implements ListView.MultiChoiceModeListener {

		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.list_notes_select_menu, menu);
			mode.setTitle("Select Items");
			return true;
		}

		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return true;
		}

		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			switch (item.getItemId()) {
			case R.id.delete:
				doDeleteNotes();
				mode.finish();
				break;
			default:

				break;
			}
			return true;
		}

		public void onDestroyActionMode(ActionMode mode) {
		}

		public void onItemCheckedStateChanged(ActionMode mode, int position,
				long id, boolean checked) {
			if (checked) {
				mListNotesAdapter.setNewSelection(position);
			} else {
				mListNotesAdapter.removeSelection(position);
			}

			final int checkedCount = mListNotes.getCheckedItemCount();
			switch (checkedCount) {
			case 0:
				mode.setSubtitle(null);
				break;
			case 1:
				mode.setSubtitle("One note selected");
				break;
			default:
				mode.setSubtitle("" + checkedCount + " notes selected");
				break;
			}
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == REQUEST_ADD_OR_EDIT_NOTE) {
			if (resultCode == RESULT_OK) {
				refreshListView();
			}
		}
	}


	private void doDeleteNotes() {
		DbAdapter dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		ArrayList<Integer> selectedItem = mListNotesAdapter
				.getCurrentCheckedPosition();
		for (int i : selectedItem) {
			ListNotesItem item = (ListNotesItem) mListNotes
					.getItemAtPosition(i);
			dbAdapter.deleteNotes(item.getId());
		}
		dbAdapter.close();
		refreshListView();
	}

	private ArrayList<ListNotesItem> getAllNotes() {
		ArrayList<ListNotesItem> result = new ArrayList<ListNotesItem>();

		DbAdapter dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		result = dbAdapter.getAllNotes();
		for (ListNotesItem item : result) {
			ContactInfo info = ContactUtils.getContactInfo(this,
					Long.parseLong(item.getContactId()));
			item.setContactName(info.getContactName());
			item.setContactPhoto(info.getContactPhoto());
		}

		dbAdapter.close();
		return result;
	}

	private void refreshListView() {
		mListNotesAdapter.setListData(getAllNotes());
		mListNotesAdapter.notifyDataSetChanged();
	}

	public void log(String msg) {
		if (DBG) {
			Log.d(TAG, msg);
		}
	}

	public ActionMode.Callback multiChoiceMode = new ActionMode.Callback() {

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

			switch (item.getItemId()) {
			case R.id.delete:
				doDeleteNotes();
				mode.finish();
				break;

			default:
				break;
			}
			return true;
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.list_notes_select_menu, menu);
			mode.setTitle("Select Items");
			return true;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			mListNotesAdapter.clearSelection();
			mActionMode = null;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false;
		}
	};

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		int count = mListNotesAdapter.getSelectedCount();
		if (count > 0) {
			if (mActionMode == null) {
				mActionMode = startActionMode(multiChoiceMode);
			}

			switch (count) {
			case 1:
				mActionMode.setSubtitle("One note selected");
				break;
			default:
				mActionMode.setSubtitle("" + count + " notes selected");
				break;
			}
		} else if (count == 0 && mActionMode != null) {
			mActionMode.finish();
		}
	}
}
